<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 600px;
            margin: 50px auto
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
        }

        .nav li {
            flex: 1;
            border: 1px solid #333;
        }

        /* .nav li:not(:first-child) {
            border-left: 0;
        } */

        .nav li+li {
            border-left: 0;
        }

        .active {
            background-color: #6ff;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="nav">
            <!-- <li data-index="0">nav_01</li>
            <li data-index="1">nav_02</li>
            <li data-index="2">nav_03</li>
            <li data-index="3">nav_04</li>
            <li data-index="4">nav_05</li>
            <li data-index="5">nav_06</li> -->
            <li class="active">nav_01</li>
            <li>nav_02</li>
            <li>nav_03</li>
            <li>nav_04</li>
            <li>nav_05</li>
            <li>nav_06</li>
        </ul>
    </div>
</body>
<script>

    var nav = document.getElementsByClassName("nav")[0];
    var liList = nav.getElementsByTagName("li");  // [li,li,li,li,li,li]


    // 导航栏切换: 
    // 1. 被点击的导航变为活跃状态(active),之前被点击的元素清除活跃状态
    // 2. 我也不想知道之前哪一个被点击了,把所有的导航 活跃状态全部清除 再把自己变为活跃状态



    for (var i = 0; i < liList.length; i++) {  // i=> 0-5
        var li = liList[i];  //获取对应下标的元素
        // 页面加载时 遍历每一个元素 => 绑定自定义属性
        li.setAttribute("data-index", i);// 0 1 2
        li.onclick = function () {
            var index = this.dataset.index;
            console.log(this, index);

            for (var j = 0; j < liList.length; j++) {
                liList[j].className = ""; // 清除每一个导航的class名
            }

            // liList[index].className = "active";
            this.className = "active";

        }
    }





</script>

</html>